package org.csu.molediary.api.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.molediary.api.common.CommonResponse;
import org.csu.molediary.api.entity.AdminSignOn;
import org.csu.molediary.api.persistence.AdminSignOnMapper;
import org.csu.molediary.api.service.AdminSignOnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("AdminSignOnService")
public class AdminSignOnServiceImpl implements AdminSignOnService {

    @Autowired
    AdminSignOnMapper adminSignOnMapper;

    @Override
    //管理员登录
    public CommonResponse<String> adminLogin(String adminname, String password) {
        QueryWrapper<AdminSignOn> adminSignOnQueryWrapper = new QueryWrapper<>();
        adminSignOnQueryWrapper.eq("adminname",adminname);
        AdminSignOn adminSignOn = adminSignOnMapper.selectOne(adminSignOnQueryWrapper);
        if(adminSignOn == null){
            return CommonResponse.createForAdminError("用户名不存在");
        }
        if(!adminSignOn.getPassword().equals(password)){
            return CommonResponse.createForAdminError("密码错误");
        }
        return  CommonResponse.createForAdminSuccessMessage("登陆成功");
    }

    @Override
    public CommonResponse<String> adminChangePassword(String adminname, String password, String newpassword) {
        QueryWrapper<AdminSignOn> adminSignOnQueryWrapper = new QueryWrapper<>();
        adminSignOnQueryWrapper.eq("adminname",adminname);
        AdminSignOn adminSignOn = adminSignOnMapper.selectOne(adminSignOnQueryWrapper);
        if(!adminSignOn.getPassword().equals(password)) {
            return CommonResponse.createForAdminError("密码错误");
        }
        else {
            adminSignOn.setPassword(newpassword);
            adminSignOnMapper.update(adminSignOn,adminSignOnQueryWrapper);
            return CommonResponse.createForAdminSuccessMessage("修改成功");
        }
    }
}
